/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.ajz.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ajz.entity.AjzModel;
import com.jeesite.modules.ajz.service.AjzModelService;

/**
 * 模板主体Controller
 * @author cww
 * @version 2019-03-28
 */
@Controller
@RequestMapping(value = "${adminPath}/ajz/ajzModel")
public class AjzModelController extends BaseController {

	@Autowired
	private AjzModelService ajzModelService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public AjzModel get(String id, boolean isNewRecord) {
		return ajzModelService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ajz:ajzModel:view")
	@RequestMapping(value = {"list", ""})
	public String list(AjzModel ajzModel, Model model) {
		model.addAttribute("ajzModel", ajzModel);
		return "modules/ajz/ajzModelList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ajz:ajzModel:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<AjzModel> listData(AjzModel ajzModel, HttpServletRequest request, HttpServletResponse response) {
		ajzModel.setPage(new Page<>(request, response));
		Page<AjzModel> page = ajzModelService.findPage(ajzModel); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ajz:ajzModel:view")
	@RequestMapping(value = "form")
	public String form(AjzModel ajzModel, Model model) {
		model.addAttribute("ajzModel", ajzModel);
		return "modules/ajz/ajzModelForm";
	}

	/**
	 * 保存模板主体
	 */
	@RequiresPermissions("ajz:ajzModel:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated AjzModel ajzModel) {
		ajzModelService.save(ajzModel);
		return renderResult(Global.TRUE, text("保存模板主体成功！"));
	}
	
	/**
	 * 删除模板主体
	 */
	@RequiresPermissions("ajz:ajzModel:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(AjzModel ajzModel) {
		ajzModelService.delete(ajzModel);
		return renderResult(Global.TRUE, text("删除模板主体成功！"));
	}

	/**
	 * 隐患类型排序
	 */
	@RequiresPermissions("ajz:ajzModel:edit")
	@RequestMapping(value = "mSort")
	public String sort(AjzModel ajzModel,Model model){
		if (ajzModel.getIsNewRecord()){
			ajzModel.setModelSort((int)ajzModelService.findCount(ajzModel)*10);
		}
		model.addAttribute("ajzModel",ajzModel);
		return "modules/ajz/ajzModelSort";
	}
	
}